package com.iflytek.ci.qams.service.impl;

/**
 * <p>
 * comment service implements.
 * </p>
 *
 * @author mdzhang
 * @since JDK 1.5
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.iflytek.ci.common.service.MailService;
import com.iflytek.ci.cpms.dao.ProjectDao;
import com.iflytek.ci.cpms.domain.Project;
import com.iflytek.ci.main.dto.MailContent;
import com.iflytek.ci.qams.dao.CommentActionDao;
import com.iflytek.ci.qams.dao.CommentDao;
import com.iflytek.ci.qams.dao.EasyCommentDao;
import com.iflytek.ci.qams.dao.PriopityDao;
import com.iflytek.ci.qams.domain.Comment;
import com.iflytek.ci.qams.domain.Issue;
import com.iflytek.ci.qams.domain.Priopity;
import com.iflytek.ci.qams.dto.CommentAction;
import com.iflytek.ci.qams.service.CommentService;
import com.iflytek.ci.qams.service.IssueService;
import com.iflytek.framework.pagination.PaginationCriteria;
import com.iflytek.framework.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service("commentService")
@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
public class CommentServiceImpl implements CommentService {

	/** 回复DAO */
	@Autowired
	private CommentDao commentDao;
	/** 问题服务 */
	@Autowired
	private IssueService issueService;
	/** 邮件通知 */
	@Qualifier("mimeMailService")
	@Autowired
	private MailService mailService;
	/** 项目服务 */
	@Autowired
	private ProjectDao projectDao;
	/** 优先级dao */
	@Autowired
	private PriopityDao priopityDao;
	/** batch comment Service */
	@Autowired
	private EasyCommentDao easyCommentDao;
	@Autowired
	private CommentActionDao commentActionDao;
	
	@Transactional
	@Override
	public int add_comment(Comment comment, long issue_id) {
		Issue issue = issueService.getFromId(issue_id);
		Date date = new Date();
		comment.setCreated(date);
		comment.setLable(issue.getIssuelabel());
		comment.setIssue(issue_id);
		int size = issue.getComment_size() + 1;
		issueService.comment_size(size, issue_id);
		commentDao.addIssue_comment(comment);
		issue.setLast_comment_time(date);
		issueService.update_issue(issue);
		if (issue.isMail_notify()) {
			final Project project = projectDao.project_with_id(issue.getPid());
			final Priopity priopity = priopityDao.get_priopity(issue
					.getPriopity());
			// 异步发送邮件
			final MailContent mailContent = new MailContent(issue, project,
					comment, priopity);
			mailService.sendNotificationMail(mailContent,
					issue.getReporterMail());
		}
		return size;
	}

	@Transactional
	@Override
	public int add_comments(Comment comment, String[] ids) {
		List<Comment> comments = new ArrayList<Comment>();
		for (String id : ids) {
			// 转换id类型
			long long_id = Long.parseLong(id);
			// 获取单个issue信息
			Issue issue = issueService.getFromId(long_id);
			// 设置tempcomment信息（日期、回复人、标签、所属问题 ..）
			Comment tempComment = new Comment();
			tempComment.setContent(comment.getContent());
			tempComment.setUid(comment.getUid());
			tempComment.setUname(comment.getUname());
			Date date = new Date();
			tempComment.setCreated(date);
			tempComment.setLable(issue.getIssuelabel());
			tempComment.setIssue(long_id);
			// 更新issue 信息（回复条数、最后一次回复）
			int size = issue.getComment_size() + 1;
			issueService.comment_size(size, long_id);
			issue.setLast_comment_time(date);
			issueService.update_issue(issue);

			// 回复加入列表以方便下面easyCommentDao批量添加
			comments.add(tempComment);
		}
		easyCommentDao.add_comments(comments);
		return 0;
	}

	@Override
	public PaginationResult<Comment> get_commentByIssue_id(
			PaginationCriteria criteria, long issue_id) {

		return commentDao.pageIssue_comment(criteria, issue_id);
	}

	@Transactional
	@Override
	public void remove_comment(long u_id, long comment_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", comment_id);
		Comment comment = commentDao.getIssue_comment(map);
		if (u_id == comment.getUid()) {
			long[] ids = new long[] { comment_id };
			commentDao.removeIssue_comment(ids);
		}
	}

	@Override
	public Comment comment_with_id(long id) {
		return commentDao.getById(id);
	}

	@Override
	public PaginationResult<CommentAction> page_commentaction_by_issuekey(
			long issue_id, PaginationCriteria criteria) {
		Issue issue = issueService.getFromId(issue_id);
		String issue_key = issue.getPkey();
		PaginationResult<CommentAction> commentAction = commentActionDao
				.page_comment_action(issue_key, criteria);
		return commentAction;
	}
}
